/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.abnf.generator;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;

import de.thwildau.tm.moses.abnf.abnf.Rule;
import de.thwildau.tm.moses.abnf.generator.model.AbnfRepeat;
 

/**
 * Helper class for ABNF rules.
 *
 * @author Thomas Kistel
 * @version $Revision$
 *
 */
/* package */ class ModelUtil {
    
    public static final String ABNF_FILE_EXTENSION = ".abnf";
    public static final String XABNF_FILE_EXTENSION = ".xabnf";
    
    public static final String CORE_RULE_FILE = "core-rules" + ABNF_FILE_EXTENSION;
    
    
    private static Set<String> CORE_RULE_NAMES = new HashSet<String>();
    private static Map<String, String> CORE_RULE_REFERENCES = new HashMap<String, String>();
    
    static {
        CORE_RULE_REFERENCES.put("ALPHA", "java.lang.String");
        CORE_RULE_REFERENCES.put("CHAR", "java.lang.String");
        CORE_RULE_REFERENCES.put("DIGIT", "java.lang.Integer");
        CORE_RULE_REFERENCES.put("VCHAR", "java.lang.String");
        
        CORE_RULE_NAMES.add("CR");
        CORE_RULE_NAMES.add("LF");
        CORE_RULE_NAMES.add("CRLF");
        CORE_RULE_NAMES.add("SP");
        CORE_RULE_NAMES.add("HTAB");
        CORE_RULE_NAMES.add("DQUOTE");
    }
    
    public static Rule findRule(EObject object) {
        EObject current = object;
        while (current != null && !(current instanceof Rule))
            current = current.eContainer();
        return (Rule)current;
    }
    
    public static boolean isSimpleCoreRule(Rule rule) {
        return isCoreRule(rule) && CORE_RULE_NAMES.contains(rule.getName());
    }
    
    public static boolean isCoreRule(Rule rule) {
        if(isCoreResource(rule)) {
            if(CORE_RULE_NAMES.contains(rule.getName()) || CORE_RULE_REFERENCES.containsKey(rule.getName()))
                return true;
            else
                throw new IllegalArgumentException("The core-rule " + rule.getName() + " is not supported");
        }
        return false;
    }
    
    public static boolean isCoreRuleReference(String ruleName) {
            if(CORE_RULE_REFERENCES.containsValue(ruleName))
                return true;
            else
            	return false;
    }
    
    public static String getCoreRuleReference(String name) {
        return CORE_RULE_REFERENCES.get(name);
    }
    
    private static boolean isCoreResource(Rule rule) {
        Resource resource = rule.eResource();
        String lastSegment = resource.getURI().lastSegment();
        return CORE_RULE_FILE.equals(lastSegment);
    }
    
    public boolean isOptional(AbnfRepeat repeat) {
        return repeat.getLowerBound() == 0 && repeat.getUpperBound() == 1;
    }
    
    public boolean isLoop(AbnfRepeat repeat) {
        return repeat.getUpperBound() > 1;
    }
    
    public boolean isInfinite(AbnfRepeat repeat) {
        return repeat.getUpperBound() == Long.MAX_VALUE;
    }
}
